package org.cttic.visualization.visualization.util;

import cn.hutool.json.JSONUtil;
import org.cttic.visualization.visualization.common.EsDataIncomplete;
import org.cttic.visualization.visualization.common.EsDataStrip;
import org.cttic.visualization.visualization.common.RouteIdentificationData;
import org.cttic.visualization.visualization.topology.GraphData;
import org.cttic.visualization.visualization.topology.service.GraphDistanceDijkstra;
import org.cttic.visualization.visualization.topology.vo.MinStep;
import org.cttic.visualization.visualization.z_service.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author shkstart
 * @create 2020-05-13 13:59
 */
@Component
public class ESQueryUtil {
    @Autowired
    private EmployeeRepository employeeRepository;
    @Autowired
    jdbcUtil jdbcUtil;
    @Autowired
    GraphDistanceDijkstra graphDistanceDijkstra;
    //时间段查询
    public void esQuery(String vehPlate,String vehPlateColor,String startTime, String endTime,List<RouteIdentificationData.Identification> list,RouteIdentificationData rtData,String exStationId){
        List<EsDataStrip> esDataStrips = employeeRepository.findByVehicleNoAndVehicleColorAndTansTimeBetween(vehPlate,vehPlateColor,startTime, endTime);
//        List<EsDataStrip> sortList = esDataStrips.stream().sorted(Comparator.comparing(EsDataStrip::getTansTime)).collect(Collectors.toList());

        if(esDataStrips!=null&&esDataStrips.size()>0){
            rtData.setIsOnline("1");
            for (EsDataStrip esDataStrip : esDataStrips) {
                RouteIdentificationData.Identification identification = new RouteIdentificationData.Identification();
                String pointType = DataUtil.getPointType( esDataStrip.getGantryType());

                identification.setTransTime(DateUtil.timeToT(esDataStrip.getTansTime()));
                identification.setEndLat(esDataStrip.getTravelId());
                identification.setPointType(esDataStrip.getGantryType()+"");
                identification.setGantryId(esDataStrip.getGantryId());
                identification.setGantryName(esDataStrip.getPointName());
//                identification.setEnExFlag(esDataStrip.get);
                //查询mysql基础信息
                jdbcUtil.mysqlUtile(Integer.parseInt(pointType),esDataStrip.getGantryId(),identification);
                list.add(identification);
            }
        }else {
            List<EsDataIncomplete> byTansTimeIsBetween = employeeRepository.findByVehicleNoAndTansTimeBetweenAndVehicleColor(vehPlate,startTime, endTime,vehPlateColor);
            List<EsDataIncomplete> sortList = byTansTimeIsBetween.stream().sorted(Comparator.comparing(EsDataIncomplete::getTansTime)).collect(Collectors.toList());

            if(sortList!=null&&sortList.size()>0){
                rtData.setIsOnline("0");
                RouteIdentificationData.Identification identification = new RouteIdentificationData.Identification();
                EsDataIncomplete esDataIncomplete = sortList.get(sortList.size() - 1);  //取最新数据
                String pointType = DataUtil.getPointType( esDataIncomplete.getGantryType());
                identification.setTransTime(DateUtil.timeToT(esDataIncomplete.getTansTime()));
                identification.setEndLat(esDataIncomplete.getTravelId());
                identification.setPointType(esDataIncomplete.getGantryType()+"");
                identification.setGantryId(esDataIncomplete.getGantryId());
                identification.setGantryName(esDataIncomplete.getPointName());
                //查询mysql基础信息
                jdbcUtil.mysqlUtile(Integer.parseInt(pointType),esDataIncomplete.getGantryId(),identification);
                list.add(identification);

                //拓扑
                String start = esDataIncomplete.getGantryId();

                if(!GraphData.startEnd.contains(start+"_"+exStationId)){
                    MinStep minStep = graphDistanceDijkstra.getMinStep(start, exStationId, GraphData.fenceStationGraph);

                    if(minStep.isReachable()){
                        LinkedList<String> step = minStep.getStep();
                        step.removeFirst();
                        step.removeLast();
                        for (String topoloGantryId : step) {
                            identification.setTransTime(DateUtil.timeToT(esDataIncomplete.getTansTime()));
                            identification.setEndLat(esDataIncomplete.getTravelId());
                            identification.setPointType(esDataIncomplete.getGantryType()+"");
                            identification.setGantryId(topoloGantryId);
                            identification.setGantryName(esDataIncomplete.getPointName());
                            jdbcUtil.mysqlUtile(Integer.parseInt(pointType),topoloGantryId,identification);
                            list.add(identification);
                        }

                    }

                }

            }
        }

    }
}
